import random

from django.shortcuts import render, HttpResponse
from django.conf import settings

from utils.tencent.sms import send_sms_single

from web.forms import RegisterModelForm


def send_sms(request):
    """ 发送短信 """
    tpl = request.GET.get("tpl")
    template_id = settings.TENCENT_SMS_TEMPLATE.get(tpl, None)
    if template_id is None:
        return HttpResponse("模板不存在！")

    code_list = []
    code = random.randrange(1000, 9999)
    code_list.append(code)
    res = send_sms_single("18008729", template_id, code_list)
    print(res)
    if res["result"] == 0:
        return HttpResponse("发送成功！")
    else:
        return HttpResponse(res["errmsg"])


def register(request):
    form = RegisterModelForm
    return render(request, "register.html", {"form": form})
